define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        Monkey = require("./monkey"),
        config = app.config,
        method = app.method;
        require("./jq_pagination");
        var jscolor=require("jscolor");
        

        if($("#coupons_set").length){  
            jscolor.install();
            $('#myTab a:first').tab('show');

            $("select#faceValue").on("change",function(){
                var nextBox = $(this).parent().next("div");
                if($(this).find("option:selected").data("val") == 0){
                    nextBox.removeClass("hide");
                }else{
                    nextBox.addClass("hide");
                }
            });


            $("input.receiveBox").on("click",function(){
                var className = $(this).data("show");
                if($(this).is(":checked")){
                    $("."+className).removeClass("hide");
                }else{
                    $("."+className).addClass("hide");
                }
            })

            $("input.condition").on("click",function(){
                if($(this).data("type") == 0){
                    $(this).closest("div.col-sm-2").next("div.must").find("input[type='text']").attr("disabled","disabled").val("");
                }else{
                    $(this).closest("div.col-sm-2").next("div.must").find("input[type='text']").removeAttr("disabled");
                }
            })

            $("input.selectRange").on("click",function(){
                if($(this).data("type") == "select"){
                    $("#select-commodity").removeAttr("disabled");
                }else{
                    $("#select-commodity").attr("disabled","disabled");
                }
            })

            $("#advancedSettings").on("click",function(){
                if($("div.js_hidden").hasClass("hide")){
                    $("div.js_hidden").removeClass("hide");
                    $(this).html("收起");
                }else{
                    $("div.js_hidden").addClass("hide");
                    $(this).html("高级设置");
                }
            })


            var SelecteFun = function(opts) {
                this.selectedArray = JSON.parse($("#beenSelected").val());
                this.num = $("span.select-num").html();
                this.oldNum = [];
                this.opts = $.extend(true,{
                    firstUrl:"/Plus/prefer1.php",
                    partUrl:"/Plus/prefer.php",
                    listUrl:""
                },opts||{});
                this.init();
            }

            SelecteFun.prototype = {
                init:function(){
                    var _this = this;
                    _this.create({});
                    _this._click();
                    _this.isAjax();
                    _this.searchF();
                    _this.saveD();
                },
                create:function(searchData){
                    var _this = this;
                    Monkey.post(_this.opts.firstUrl,function(d){
                        if(d){
                            $("#search").removeAttr("disabled");
                            $("#myTab .total-box").html(d.total);
                            $("span.select-num").html(d.select);
                            var $html = template("all_template",d);
                            $('#all-list-box').append($html);

                            var initPagination = function() {
                                // 创建分页
                                $("#Pagination").pagination(d.chief, {
                                    num_edge_entries: 1, //边缘页数
                                    num_display_entries: 4, //主体页数
                                    callback: pageselectCallback,
                                    items_per_page: 1, //每页显示1项
                                    prev_text: "&laquo",
                                    next_text: "&raquo",
                                    load_first_page: true //第一次自动加载,true(默认),false(不加载)
                                });
                             };

                            var pageselectCallback = function(page_index, jq){
                                
                                $('#all-list-box div.list-box').hide();
                                if(!$('#all-list-box div.list-box:eq(' + page_index + ')').data("crate")){
                                    Monkey.post(_this.opts.listUrl,function(datas){
                                        var $html = template("list_template",datas);
                                        $('#all-list-box div.list-box:eq(' + page_index + ')').find("ul").append($html);
                                        $('#all-list-box div.list-box:eq(' + page_index + ')').data("crate",true);
                                    },'',null,{page:page_index+1,data:$("#commodity-box").serialize()})
                                }
                                $('#all-list-box div.list-box:eq(' + page_index + ')').show(); 
                                //阻止单击事件
                                return false;
                            }
                            initPagination();
                        }

                    },'',null,searchData);
                },
                _click:function(){
                    var _this = this;
                    $(document).on("click","li.itmes",function(){
                        var cid = $(this).data("cid");
                        var activeItme = $(this).find("a");
                            activeItme.toggleClass("active");
                            $(this).toggleClass("active");

                        if($(this).hasClass("active")){
                            _this.selectedArray.push(cid);
                            _this.animateF($(".fly-num"),true);
                        }else{
                            for(var i=0;i<_this.selectedArray.length;i++){
                                if(_this.selectedArray[i] == cid){
                                    _this.selectedArray.splice(i,1);
                                }
                            }
                            _this.animateF($(".fly-num"),false);
                        }

                        if(_this.oldNum.length != _this.selectedArray.length){
                            $("a.select-box").data("ajax",false);
                        }else{
                             if(_this.oldNum.sort().toString() == _this.selectedArray.sort().toString()){
                                $("a.select-box").data("ajax",true);
                            }else{
                                $("a.select-box").data("ajax",false);
                            }
                        }
                        $("span.select-num").html(_this.selectedArray.length);
                    })
                },
                isAjax:function(){
                    var _this = this;
                    $(document).on("click","#myTab a.select-box",function(){
                        var $this = $(this);
                        if($this.data("ajax")){return}
                        Monkey.post(_this.opts.partUrl,function(d){
                            if(d){
                                $this.data("ajax","true");
                                $("span.select-num").html(d.select);
                                $("#beenSelected").val(_this.selectedArray);
                                _this.oldNum = JSON.parse("["+$("#beenSelected").val()+"]");
                                var $html = template("js_template",d);
                                //$("#hiddenresult").empty().append($html);
                                //var length = $("#hiddenresult li").length;

                                $("#Searchresult").empty().append($html);
                                var length = $("#Searchresult li").length;

                                var initPagination = function() {
                                    var num_entries = $("#Searchresult li").length;
                                    // 创建分页
                                    $("#Pagination1").pagination(num_entries, {
                                        num_edge_entries: 1, //边缘页数
                                        num_display_entries: 4, //主体页数
                                        callback: pageselectCallback,
                                        items_per_page: 5, //每页显示1项
                                        prev_text: "&laquo",
                                        next_text: "&raquo"
                                    });
                                 };

                                var pageselectCallback = function(page_index, jq){

                                    //注视的代码是 清空的方法
                                    var items_per_page = 5;
                                    var max_elem = Math.min((page_index+1) * items_per_page, length);
                                    //$("#Searchresult").html("");
                                    $('#Searchresult li').addClass("hide");
                                    // 获取加载元素
                                    for(var i=page_index*items_per_page;i<max_elem;i++){
                                        //$("#Searchresult").append($("#hiddenresult li:eq("+i+")").clone());
                                        $('#Searchresult li:eq(' + i + ')').removeClass("hide"); 
                                    }
                                    //阻止单击事件
                                    return false;
                                }
                                initPagination();
                            }
                        },"div.tab-content",null,{select:_this.selectedArray});
                    })
                },
                searchF:function(){
                    var _this = this;
                    $(document).on("click","#search",function(){
                        $("#all-list-box").empty();
                        _this.create($("#commodity-box").serialize());
                    })
                },
                animateF:function(obj,bools){
                    var _this = this;
                    var $html = "";
                    if(bools){
                        $html = "+1";
                    }else{
                        $html = "-1"
                    }
                    $(".fly-num").html($html).css("display","block").stop().animate({
                        "top":"-25px",
                        "right":"15px",
                        "opacity":0,
                        "fontSize":"14px"
                    },600,function(){
                        $(this).css({"top":"-5px","right":"25px","display":"none","opacity":1,"font-size":"10px"});
                    });
                },
                saveD:function(){
                    var _this = this;
                    $(document).on("click","#saveData",function(){
                        $("#beenSelected").val(_this.selectedArray)
                    })
                }
            }

            if(window.config){
                new SelecteFun({
                    firstUrl:window.config.firstdata,
                    partUrl:window.config.partdata,
                    listUrl:window.config.listdata
                });
            }
            

            $("#select-commodity").on("click",function(){
                $("#js_question_modal").modal("show");
            })
            
        }
});